package com.example.junit;

import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

import com.example.domain.single.Boy;
import com.example.ormlite.DBHelper_DataDir;
import com.example.utils.ORMLiteTableUtils;
import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.table.TableInfo;

public class ORMLiteTableUtils_test extends AndroidTestCase{
	private Context context = null;
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		context = getContext();
	}
	
	
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	
	
	

	public void testAlterTableColumnAndColumnType_SQL3() throws SQLException {
		Class clazz = Boy.class;
		
		DBHelper_DataDir dbHelper_DataDir = DBHelper_DataDir.getInstance(context);
		
		SQLiteDatabase db = dbHelper_DataDir.getWritableDatabase();
		String sql_columns_format = "select * from %s limit 0,1";
		String sql_columns = String.format(sql_columns_format, clazz.getSimpleName());
		Cursor cursor = db.rawQuery(sql_columns, new String[]{});
		String[] columns = cursor.getColumnNames();
		
		TableInfo tableInfo = ((BaseDaoImpl<?, ?>) dbHelper_DataDir.getDao(clazz)).getTableInfo();
		
		Set<String> entity_all_columns = new HashSet<String>();
		
		Set<String> db_all_columns = new HashSet<String>(Arrays.asList(columns));
		
		Set<String> db_need_add_columns = new HashSet<String>();
		Set<String> db_need_delete_columns = new HashSet<String>(Arrays.asList(columns));
		
		FieldType[] fieldTypes = tableInfo.getFieldTypes();
		String fieldName = "";
		for (FieldType fieldType : fieldTypes) {
			fieldName = fieldType.getColumnName();
			entity_all_columns.add(fieldName);
		}
		
		db_need_add_columns.addAll(entity_all_columns);
		
		db_need_add_columns.removeAll(db_all_columns);
		db_need_delete_columns.removeAll(entity_all_columns);
		
		
		System.out.println("db_need_add_columns:"+db_need_add_columns);
		System.out.println("db_need_delete_columns:"+db_need_delete_columns);
		
		Set<FieldType> db_need_add_columns_FieldType = new HashSet<FieldType>();
		Set<FieldType> db_need_delete_columns_FieldType = new HashSet<FieldType>();
		
		for (FieldType fieldType : fieldTypes) {
			fieldName = fieldType.getColumnName();
			for (String db_need_add_column : db_need_add_columns) {
				if (fieldName.equalsIgnoreCase(db_need_add_column)) {
					db_need_add_columns_FieldType.add(fieldType);
				}
			}
			
			for (String db_need_delete_column : db_need_delete_columns) {
				if (fieldName.equalsIgnoreCase(db_need_delete_column)) {
					db_need_delete_columns_FieldType.add(fieldType);
				}
			}
		}
		
		if (!db_need_add_columns_FieldType.isEmpty()) {
			FieldType[] fieldTypes_temp = new FieldType[]{} ; 
			fieldTypes_temp = db_need_add_columns_FieldType.toArray(fieldTypes_temp);
			
			String sql_alter_columnAndColumnType_str = ORMLiteTableUtils.alterTableColumnAndColumnTypeByFieldTypes_SQL(dbHelper_DataDir.getConnectionSource(), clazz,fieldTypes_temp);
			System.out.println("sql_alter_columnAndColumnType_str:"+sql_alter_columnAndColumnType_str);
			
			String sql_alter_format = "ALTER TABLE `%s` ADD COLUMN %s ;";
				
				
			String sql_alter = String.format(sql_alter_format, tableInfo.getTableName(),sql_alter_columnAndColumnType_str);
			System.out.println("sql_alter:"+sql_alter);
			db.execSQL(sql_alter);
		}
		
	}


	
}
